Working Principle and example of javascript prototype inheritance, javascript prototype
First, we will share with you examples of JS prototype inheritance for your reference. The details are as follows:
I. JS prototype inheritance
This article mainly introduces the javascript Inheritance Mechanism and analyzes in detail the principle of javascript inheritance and the inheritance Implementation Method Based on prototype chain in the form of examples, which has good reference value, for more information
This article mainly introduces the inheritance Implementation Method of Javascript programming, and summarizes five common inheritance Techniques Based on instance form analysis, which has some reference value, if you need it, refer to the examples in this article to illustrate the inheritance of
This article mainly introduces the use of javascript prototype chain inheritance. The example analyzes the skills and precautions in javascript prototype chain inheritance, which is of great practical value, if you need it, you can refer to the examples in this article to an
This article mainly introduces the use of javascript prototype chain Inheritance. The example analyzes the skills and precautions in javascript prototype chain inheritance, which is of great practical value, if you need it, you can refer to the examples in this article to an
This article mainly introduces in detail javascript prototype inheritance, clarifies what prototype inheritance is, and how to use prototype inheritance in JavaScript, if you are interested, you can refer to the example of JS prot
First for everyone to share JS prototype Inheritance example, for your reference, the specific content as follows
First, JS prototype inheritance
Second, JavaScript prototype inheritance working principle
JavaScript
This example describes the JavaScript inheritance mechanism. Share to everyone for your reference. The specific analysis is as follows:
Beginner JavaScript is generally difficult to understand the inheritance mechanism of JavaScript
I have been hard to understand the Inheritance Mechanism of Javascript language. It does not have the concept of \ "subclass \" or \ "parent class \", nor does it have the concept of \ "class \" (class) and \ "instance \" (instance), all rely on a very strange \ "prototype chain \" (prototype chain) mode, to achieve inheritance.
I spent a lot of time learning thi
Tree-structured data is often used in writing libraries, and some tree-structured data is very demanding from root to leaf paths. For example, the entire routing table of a site is such a tree, and its "path" is actually the path part of the URL. So I have a few times with a lunatic array of inheritance to achieve, below to share with you.In JavaScript, an array
The difficulty of JavaScript is object-oriented programming, which describes two ways to inherit javascript: JavaScript advanced inheritance, which uses an example to show how JS object-oriented programming and how to implement inheritan
The example in this article analyzes the usage of inheritance in JavaScript. Share to everyone for your reference. Specifically as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The
//define the person Class function person () {} Person.prototype.walk = function () {alert (' I am walking! '); Person.prototype.sayHello = fu
created for the example:
This code creates a triangle with a base of 12, a height of 4, and a rectangle with a length of 22 and a width of 10. The number and area of each shape are then output, proving that the sides property is assigned correctly, and the Getarea () method returns the correct value. The area of the triangle should be 24, and the area of the rectangle should be 220.
4. How about using a dynamic prototyping method?
The previous
Example
The code is as follows
Copy Code
/*** Implementation subclass inherits the parent class, but does not produce redundant properties and methods* @returns {Function}*/Define (function () {return function (subtype, supertype) {var proto = new Object (supertype.prototype);Proto.constructor = subtype;Subtype.prototype = Proto;};});——————————————————————————Define (function () { function ostring (s){This.str = s;This.len
that you cannot share properties and methods.
For example, in the constructor of a dog object, set the common property species of an instance object.
The code is as follows
Copy Code
function DOG (name) {THIS.name = name;this.species = ' canine Division ';}
Then, two instance objects are generated:
The code is as follows
Copy Code
var Doga = new DOG (' hairy ');var dogb = new DOG ('
As an object-oriented language, then the inheritance of nature is a big feature, the following is a very simple code example, it demonstrates the rationale for inheritance, interested or just want to learn this aspect of friends can refer to, hope to be able to help the group.
Inherited
function Person (name,sex)
{
this.name=name;
this.sex=sex;
}
Person.pr
Create a custom object with the prototype prototype person:functionPerson (name,sex) { This. Name =name; This. Sex =sex;} Person.prototype={getName:function(){return This. Name}, Getsex:function(){return This. Sex}}varLiu =NewPerson ("LCY", "female");//Create a blank object//Copy the attributes in Person.prototype to an empty object (internally implemented as a hidden link)//pass the object through the This keyword to the constructor and execute the constructor//assign this object to the obje
() {Console.log ("Hi");};return clone;}var person = {Name: "Nicholas",Friends: ["Shelby", "Court", "Van"]};var Anotherperson = Createanother (person);Anotherperson.sayhi ();
What does not understand is why the sayhi defined here cannot be shared to person.
And parasitic modular inheritance now seems to be called the most efficient way (-_-actually I don't understand)
code is as follows
copy code
functi
number of edges of the object to the constructor of Polygon. Then, assign the base and height values to the appropriate attributes.
After the prototype chain inheritance method is used, Triangle will overwrite the getArea () method and provide custom calculation for the Triangle Area.
The last class is Rectangle, which also inherits Polygon. A rectangle has four sides. The area is calculated by length x width. The length and width are required for th
Object Creation:
When a function object is created, the function object produced by the functions constructor runs code similar to this:
Copy Code code as follows:
This.prototype={constructor:this};
Suppose function f
f when constructing an object in new form, the object's constructor is set to this F.prototype.constructor
If the function modifies the prototype of the function before the object is created, it affects the Construtor property of the Created object
Such as
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.